#include "Scrotum.h"

Scrotum::Scrotum(Artman *artman)
{
	m_size=0;
	m_visible=false;
	this->setTexture(*artman->getTexture("sack.png"));
	this->setTextureRect(sf::Rect <int> (0,0,64,64));
	m_overlay.setTexture(*artman->getTexture("sack.png"));
	m_overlay.setTextureRect(sf::Rect<int>(64,0,64,64));
	m_overlay.setColor(sf::Color(255,255,255,200));

	m_contents.setOrigin(32,32);
	m_overlay.setOrigin(32,32);
	this->setOrigin(32,32);

}

void Scrotum::Consume(sf::Texture *miniview)
{

	m_stage=0;
	m_counter=0;
	m_contents.setTexture(*miniview);
	m_contents.setTextureRect(sf::Rect <int> (0,0,64,64));
}

bool Scrotum::Digest(float power)
{
	m_size+=power;
	m_counter+=power;
	if (m_counter>10)
	{
		m_counter=0;
		if (m_stage<2)
		{
			m_stage++;
			m_contents.setTextureRect(sf::Rect<int>(m_stage*64,0,64,64));
		}
		else
		{
			return true;
		}
	}
	int alpha=(m_counter*8)+(m_stage*85);
	if (alpha<=255)
	{
		m_overlay.setColor(sf::Color(255,255,255,alpha));
	}
	return false;
}
	
void Scrotum::Draw(sf::RenderWindow &window)
{
	if (m_visible)
	{
		window.draw(m_contents);
		window.draw(m_overlay);
		window.draw(*this);
	}
}

void Scrotum::Update(float dt, sf::Vector2f pos)
{
	if (m_visible)
	{
		
		m_size+=(dt/5);
		if (m_size>2*3.14159265)
		{
			m_size=0;
		}
		float s=sin(m_size)*0.05 + 1;
        float t=sin(m_size+3.141592*0.5)*0.05 + 1;
		m_overlay.setScale(s,t);
		this->setScale(s,t);
		m_contents.setScale(s,t);
		
		this->setPosition(pos);
		m_contents.setPosition(pos);
		m_overlay.setPosition(pos);
	}
}

bool Scrotum::getVisible()
{
	return m_visible;
}

void Scrotum::setVisible(bool vis)
{
	m_visible=vis;
}